﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CentaLine.Infrastructure;
using CentaLine.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace CentaLine.DAL
{
    public class CounselorDAL
    {
        Database db = DBHelper.CreateDataBase();
        Database dbOAS = DBHelper.CreateDataBase("CentalineConnectOAS"); //人事信息库

        #region 废弃　 2013-10-12 14:36:33 　 根据业务员信息获取其上级总监的信息,以及申请对应月份
        ///// <summary>
        ///// 根据业务员信息获取其上级总监的信息,以及申请对应月份
        ///// </summary>
        ///// <param name="counselor">业务员</param>
        ///// <returns></returns>
        //public Director GetDirectorInfoByCounselorDAL(string  counselorId) //TODO 此处正式环境 肯定要改 获取组织架构关系
        //{
        //    Director director = null;
        //    StringBuilder sb = new StringBuilder();
        //    sb.Append("SELECT TOP 1 RC,RCCODE ");
        //    sb.Append("FROM [VTPhoneDB].[dbo].[VPMS.EShopApply] ");
        //    sb.Append("where User_Code=@User_Code ");
        //    sb.Append("order by ApplyMonth desc");
        //    DbCommand cmd = db.GetSqlStringCommand(sb.ToString());
        //    db.AddInParameter(cmd, "User_Code", DbType.String, counselorId);
        //    using (IDataReader dataReader = db.ExecuteReader(cmd))
        //    {
        //        while (dataReader.Read())
        //        {
        //            director = new Director();
        //            object ojb;
        //            ojb = dataReader["RC"];
        //            if (ojb != null && ojb != DBNull.Value)
        //            {
        //                director.Name = (string)ojb;
        //            }
        //            ojb = dataReader["RCCODE"];
        //            if (ojb != null && ojb != DBNull.Value)
        //            {
        //                director.Id = (string)ojb;
        //            }
        //        }
        //    }
        //    return director;
        //}
        #endregion

        public Counselor GetCounselorDAL(string counselorId)
        {     
            Counselor counselor = null;
            StringBuilder sb = new StringBuilder();
            sb.Append(@"SELECT TOP 1
              [USER_NAME]
              ,[User_code]
              ,[IsTempUser]
              ,[P1_User_NAME] 
              ,[P1_User_code]
              ,[P2_User_NAME]
              ,[P2_User_code]
              ,[P3_User_NAME]
              ,[P3_User_code]
              ,[P4_User_NAME]
              ,[P4_User_code]
              ,[P5_User_NAME]
              ,[P5_User_code]
              ,[P6_User_NAME]
              ,[P6_User_code]
            FROM [oas].[oas].[v_pub_user_parents] where user_code=@counselorId");
            DbCommand cmd = dbOAS.GetSqlStringCommand(sb.ToString());
            dbOAS.AddInParameter(cmd, "counselorId", DbType.String, counselorId);
            using (IDataReader dataReader = dbOAS.ExecuteReader(cmd))
            {
                while (dataReader.Read())
                {
                    counselor = ReaderBind(dataReader);
                }
            }
            if (counselor != null)//填充总监信息
            {
                counselor.Director = GetDirectorByCounselor(counselor);
            }

            return counselor;
        }


        private Counselor ReaderBind(IDataReader dataReader)
        {
            var counselor = new Counselor
             {
                 Id = dataReader["User_code"].ToString(),
                 Name = dataReader["USER_NAME"].ToString(),
                 IsWorking = dataReader["IsTempUser"].ToString() == "在职" ? true : false,
                 //存在多个领导
                 Director1 = new Director
                 {
                     Id = dataReader["P1_User_code"].ToString(),
                     Name = dataReader["P1_User_NAME"].ToString()
                 },
                 Director2 = new Director
                 {
                     Id = dataReader["P2_User_code"].ToString(),
                     Name = dataReader["P2_User_NAME"].ToString()
                 },
                 Director3 = new Director
                 {
                     Id = dataReader["P3_User_code"].ToString(),
                     Name = dataReader["P3_User_NAME"].ToString()
                 },
                 Director4 = new Director
                 {
                     Id = dataReader["P4_User_code"].ToString(),
                     Name = dataReader["P4_User_NAME"].ToString()
                 },
                 Director5 = new Director
                 {
                     Id = dataReader["P5_User_code"].ToString(),
                     Name = dataReader["P5_User_NAME"].ToString()
                 },
                 Director6 = new Director
                 {
                     Id = dataReader["P6_User_code"].ToString(),
                     Name = dataReader["P6_User_NAME"].ToString()
                 },
             };
            return counselor;
        }
        /// <summary>
        /// 填充总监信息
        /// </summary>
        /// <param name="counselor"></param>
        /// <returns></returns>
        private Director GetDirectorByCounselor(Counselor counselor)
        {
            Director d = null;
            var oasDirectorList = new DirectorDAL().GetAllDirectorListFromOASDAL();//获取组织库信息列表
            if (oasDirectorList.Any())
            {
                foreach (var o in oasDirectorList)
                {
                    var directorId = o.Id;
                    if (directorId == counselor.Director1.Id)
                    {
                        d = counselor.Director1;
                        break;
                    }
                    if (directorId == counselor.Director2.Id)
                    {
                        d = counselor.Director2;
                        break;
                    }
                    if (directorId == counselor.Director3.Id)
                    {
                        d = counselor.Director3;
                        break;
                    }
                    if (directorId == counselor.Director4.Id)
                    {
                        d = counselor.Director4;
                        break;
                    }
                    if (directorId == counselor.Director5.Id)
                    {
                        d = counselor.Director5;
                        break;
                    }
                    if (directorId == counselor.Director6.Id)
                    {
                        d = counselor.Director6;
                        break;
                    }
                }
            }
            return d;
        }

    }
}
